﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProcessWatch
{
    partial class WinService : ServiceBase
    {
        bool m_bStop = false;
        public WinService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            m_bStop = false;
            Task.Run(new Action(() => {
                while (!m_bStop)
                {
                    if (Process.GetProcessesByName(Application.ProductName).Length <= 1)
                        Process.Start(Application.ExecutablePath);
                    Thread.Sleep(1000);
                }
            }));
        }

        protected override void OnStop()
        {
            m_bStop = true;
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
        }
    }
}
